package com.aurora.oa.work.view;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.adapter.CommonRecyclerViewAdapter;
import com.aurora.oa.adapter.CommonRecyclerViewHolder;
import com.aurora.oa.customview.ItemDivider;
import com.aurora.oa.listener.OnRequestPatrollogListListener;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.utils.DateUtil;
import com.aurora.oa.utils.ToastUtil;
import com.aurora.oa.work.bean.PatrollogListBean;
import com.codbking.calendar.CalendarUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.DatePicker;
import cn.qqtheme.framework.picker.DateTimePicker;

/**
 * Created by Administrator on 2017/5/2 0002.
 */
public class PatrollogMainActivity extends BaseActivity {
    @Bind(R.id.tv_back_month)
    TextView tvBackMonth;
    @Bind(R.id.tv_data)
    TextView tvData;
    @Bind(R.id.tv_next_month)
    TextView tvNextMonth;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.bt_write_log)
    Button btWriteLog;
    private List<PatrollogListBean> data = new ArrayList<>();
    private CommonRecyclerViewAdapter<PatrollogListBean> adapter ;
    private Handler handler = new Handler();
    RequestHttpModel requestHttpModel = new RequestHttpModelImpl();
    LinearLayoutManager layoutManager;
    //传给服务器的日期
    String month_date;
    DatePicker datePicker;
    Date currentdate;
    Calendar curr;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitlebar(true, "巡店日志", true);
        setMyContentView(R.layout.activity_patrollog_main);
        ButterKnife.bind(this);
        initView();
        getData();
    }

    private void initView() {
        currentdate = new Date();
        curr = Calendar.getInstance();
        curr.setTime(currentdate);
        String[] dates = CalendarUtil.getYMDZero(currentdate);
        tvData.setText(dates[0]+"年"+dates[1]+"月");
        datePicker = new DatePicker(this, DateTimePicker.YEAR_MONTH);
        datePicker.setLabel("年","月","日");
        datePicker.setSelectedItem(curr.get(Calendar.YEAR),curr.get(Calendar.MONTH)+1);
        datePicker.setOnDatePickListener(new DatePicker.OnYearMonthPickListener() {
            @Override
            public void onDatePicked(String year, String month) {
                curr.set(Calendar.YEAR,Integer.parseInt(year));
                curr.set(Calendar.MONTH,Integer.parseInt(month)-1);
                currentdate = curr.getTime();
                initMonthDate(currentdate);
            }
        });
        month_date = dates[0]+"-"+dates[1]+"-"+"01";
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new CommonRecyclerViewAdapter<PatrollogListBean>(this,data) {
            @Override
            public void convert(CommonRecyclerViewHolder h, PatrollogListBean entity, int position) {
                h.setText(R.id.tv_remark,entity.shopname);
                h.setText(R.id.tv_creat_time, DateUtil.getDateTime(entity.createtime));
                String status = "";
               switch (entity.approvalstate){
                    case "1":
                        status = "未审核";
                        break;
                    case "2":
                        status = "已通过";
                        break;
                    case "3":
                        status = "已拒绝";
                        break;
                }
                h.setText(R.id.tv_status,status);
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_patrollog;
            }
        };
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new ItemDivider().setDividerWith(8).setDividerColor(Color.TRANSPARENT));

        adapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Intent intent =new Intent(PatrollogMainActivity.this,PatrollogDetailActivity.class);
                intent.putExtra("id",data.get(position).id);
                startActivity(intent);
            }
        });
    }

    //请求数据
    private void getData(){
        //patrollog/getMyPatrollogByMonth/{date}
        requestHttpModel.getPatrollogListData(month_date,new OnRequestPatrollogListListener() {
            @Override
            public void onRequestPatrollogListSuccess(List<PatrollogListBean> patrollogListBeanList) {
                    refreshUI(patrollogListBeanList);
            }

            @Override
            public void onRequestPatrollogListFail(String message) {
                ToastUtil.show(getApplicationContext(),message);
            }
        });
    }

    private void refreshUI(List<PatrollogListBean> patrollogListBeanList) {
        data.clear();
        data.addAll(patrollogListBeanList);
        adapter.notifyDataSetChanged();
    }

    @OnClick({R.id.tv_back_month, R.id.tv_data, R.id.tv_next_month, R.id.bt_write_log})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back_month:
                curr.set(Calendar.MONTH,curr.get(Calendar.MONTH)-1);
                currentdate = curr.getTime();
                initMonthDate(currentdate);
                break;
            case R.id.tv_data:
                datePicker.show();
                break;
            case R.id.tv_next_month:
                curr.set(Calendar.MONTH,curr.get(Calendar.MONTH)+1);
                currentdate = curr.getTime();
                initMonthDate(currentdate);
                break;
            case R.id.bt_write_log:
                startActivity(new Intent(PatrollogMainActivity.this,WritePatrollogActivity.class));
                break;
        }
    }

    private void initMonthDate(Date currentdate) {
        String[] dates = CalendarUtil.getYMDZero(currentdate);
        month_date = dates[0]+"-"+dates[1]+"-"+"01";
        tvData.setText(dates[0]+"年"+dates[1]+"月");
        getData();
    }
}
